package org.wlldTest.lesson8;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.wlld.config.RZ;
import org.wlld.entity.WordTwoVectorModel;
import org.wlld.function.Tanh;
import org.wlld.nerveCenter.ModelParameter;
import org.wlld.nerveCenter.NerveManager;
import org.wlld.nerveEntity.SensoryNerve;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Test {
    public static NerveManager nerveManager;

    public static void main(String[] args) throws Exception {
        init();
        //study();
        test();
    }

    public static void init() throws Exception {
        nerveManager = new NerveManager(2, 3, 2, 1,
                new Tanh(), 0.01, RZ.NOT_RZ, 0, 0);
        nerveManager.init(true, false, false);
        ModelParameter modelParameter = JSONObject.parseObject(Model.Model, ModelParameter.class);
        nerveManager.insertDnnModel(modelParameter);
    }

    public static void test() throws Exception {
        Random random = new Random();
        Back back = new Back();
        for (int i = 0; i < 10; i++) {
            System.out.println("=======================");
            double x1 = random.nextDouble() * 0.1;
            double y1 = random.nextDouble() * 0.1 + 0.1;
            double x2 = x1 + 0.5;
            double y2 = y1 + 0.5;
            double[] parameter1 = new double[]{x1, y1};
            double[] parameter2 = new double[]{x2, y2};
            //insertParameter(parameter1, false, null, back);
            insertParameter(parameter1, false, null, back);
            System.out.println("v1:" + back.getV1());
            System.out.println("v2:" + back.getV2());

        }
    }

    public static void study() throws Exception {
        Random random = new Random();
        Map<Integer, Double> E1 = new HashMap<>();
        Map<Integer, Double> E2 = new HashMap<>();
        E1.put(1, 0.1);
        E1.put(2, 0.2);
        E2.put(1, 0.7);
        E2.put(2, 0.8);
        for (int i = 0; i < 1000; i++) {
            double x1 = random.nextDouble() * 0.1;
            double y1 = random.nextDouble() * 0.1 + 0.1;
            double x2 = x1 + 0.5;
            double y2 = y1 + 0.5;
            double[] parameter1 = new double[]{x1, y1};
            double[] parameter2 = new double[]{x2, y2};
            insertParameter(parameter1, true, E1, null);
            insertParameter(parameter2, true, E2, null);
        }
        ModelParameter modelParameter = nerveManager.getDnnModel();
        String model = JSON.toJSONString(modelParameter);
        System.out.println(model);
    }

    public static void insertParameter(double[] parameter, boolean isStudy, Map<Integer, Double> E
            , Back back) throws Exception {
        List<SensoryNerve> sensoryNerves = nerveManager.getSensoryNerves();
        for (int i = 0; i < sensoryNerves.size(); i++) {
            sensoryNerves.get(i).postMessage(1, parameter[i], isStudy, E, back);
        }
    }


}
